#!/bin/sh
# Create an application-specific initscript that runs service-wrapper.

QUIET=false
PREINIT_SH=/dev/null

case "$1" in
-q|--quiet)
	QUIET=true
	;;
-h|--help)
	echo >&2 "Usage: cat <PARAMS> | $0 [-q|--quiet]"
	exit 1
esac

if ! $QUIET; then
	cat >&2 <<EOF
This script will generate an initscript that runs service-wrapper. I will now
read from STDIN; please input your parameters in the following format:

==== start of input stream ====
APP_NAME
APP_LONG_NAME
APP_DESCRIPTION
NAME1 VALUE1
NAME2 VALUE2
...
NAMEn VALUEn

PREINIT_SHELL_SCRIPT_LINE1
PREINIT_SHELL_SCRIPT_LINE2
...
PREINIT_SHELL_SCRIPT_LINEn
==== end of input stream ====

where:

APP_NAME: short system name of application, e.g. avahi-daemon, apache2
	This is used to refer to scripts like /etc/default/APP_NAME
APP_LONG_NAME: long name of application, e.g. Apache Web Server
	This is used to refer to the application in messages for the end user.
APP_DESCRIPTION: longer description for your application
	This serves as documentation for the end user.
NAMEn VALUEn: optional name-value pairs that set application-specific defaults
	for service-wrapper variables. At run-time, these may be overridden by user
	settings in /etc/default/APP_NAME. Note: a space separates NAME VALUE, not
	an equals sign (=). Also, currently VALUE cannot contain "|".
PREINIT_SHELL_SCRIPTn: at run-time, these commands will be run after the script
sources /etc/default/APP_NAME, to do further processing on any variables set.

Please enter your input; press Ctrl-D when you are done:
EOF
fi

# splice params

SED_ARGS=""
push_sed_expr() { SED_ARGS="$SED_ARGS -e '$1'"; }

read APP_NAME
push_sed_expr "s/@app.name@/$APP_NAME/g"

read APP_LONG_NAME
push_sed_expr "s/@app.long.name@/$APP_LONG_NAME/g"

read APP_DESCRIPTION
push_sed_expr "s/@app.description@/$APP_DESCRIPTION/g"

while read ARG VAL; do
	if [ -z "$ARG" ]; then break; fi
	push_sed_expr 's|^\s*#\?\s*\('"$ARG"'=\).*|\1"'"$VAL"'"|g'
done

PREINIT_SH="$(tempfile)"
cat - > "$PREINIT_SH"

if ! $QUIET; then set -x; fi
eval sed $SED_ARGS <<EOF | sed -e "/WRAPPER_PREINIT START/r$PREINIT_SH"
#! /bin/sh

#
# Copyright (c) 1999, 2010 Tanuki Software, Ltd.
# http://www.tanukisoftware.com
# All rights reserved.
#
# This software is the proprietary information of Tanuki Software.
# You shall use it only in accordance with the terms of the
# license agreement you entered into with Tanuki Software.
# http://wrapper.tanukisoftware.com/doc/english/licenseOverview.html
#
# Java Service Wrapper sh script.  Suitable for starting and stopping
#  wrapped Java applications on UNIX platforms.
#

#-----------------------------------------------------------------------------
# These settings can be modified to fit the needs of your application
# Optimized for use with version 3.5.3 of the Wrapper.

# Application
APP_NAME="@app.name@"
APP_LONG_NAME="@app.long.name@"

# Wrapper
WRAPPER_CMD="/usr/sbin/wrapper"
WRAPPER_CONF="../conf/wrapper.conf"

# Priority at which to run the wrapper.  See "man nice" for valid priorities.
#  nice is only used if a priority is specified.
PRIORITY=

# Location of the pid file.
PIDDIR="."

# If uncommented, causes the Wrapper to be shutdown using an anchor file.
#  When launched with the 'start' command, it will also ignore all INT and
#  TERM signals.
#IGNORE_SIGNALS=true

# Wrapper will start the JVM asynchronously. Your application may have some
#  initialization tasks and it may be desirable to wait a few seconds
#  before returning.  For example, to delay the invocation of following
#  startup scripts.  Setting WAIT_AFTER_STARTUP to a positive number will
#  cause the start command to delay for the indicated period of time 
#  (in seconds).
# 
WAIT_AFTER_STARTUP=0

# If set, wait for the wrapper to report that the daemon has started
WAIT_FOR_STARTED_STATUS=true
WAIT_FOR_STARTED_TIMEOUT=120

# If set, the status, start_msg and stop_msg commands will print out detailed
#   state information on the Wrapper and Java processes.
#DETAIL_STATUS=true

# If specified, the Wrapper will be run as the specified user.
# IMPORTANT - Make sure that the user has the required privileges to write
#  the PID file and wrapper.log files.  Failure to be able to write the log
#  file will cause the Wrapper to exit without any way to write out an error
#  message.
# NOTE - This will set the user which is used to run the Wrapper as well as
#  the JVM and is not useful in situations where a privileged resource or
#  port needs to be allocated prior to the user being changed.
#RUN_AS_USER=

# The following two lines are used by the chkconfig command. Change as is
#  appropriate for your application.  They should remain commented.
# chkconfig: 2345 20 80
# description: @app.long.name@

# When installing on On Mac OSX platforms, the following domain will be used to
#  prefix the plist file name.
PLIST_DOMAIN=org.tanukisoftware.wrapper
 
# Initialization block for the install_initd and remove_initd scripts used by
#  SUSE linux distributions.
### BEGIN INIT INFO
# Provides: @app.name@
# Required-Start: $local_fs $network $syslog
# Should-Start: 
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: @app.long.name@
# Description: @app.description@
### END INIT INFO

# Do not modify anything beyond this point
#-----------------------------------------------------------------------------

if [ -f "/etc/default/$APP_NAME" ]; then
	. "/etc/default/$APP_NAME"
fi

# WRAPPER_PREINIT START
# WRAPPER_PREINIT END

. "/usr/share/wrapper/daemon.sh"

EOF
